package com.example.service;

import com.example.config.FlywayConfig;
import org.flywaydb.core.Flyway;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * 初始化逻辑层
 *
 * @author: guohh
 **/
@Service
public class InitService {

    private Logger logger = LoggerFactory.getLogger(InitService.class);

    @Autowired
    private FlywayConfig config;

    @Autowired
    private DataSource dataSource;

    @PostConstruct
    public void migrate() {
        Flyway flyway = new Flyway();
        flyway.setBaselineOnMigrate(true);
        flyway.setTable(config.getTable());
        flyway.setLocations(config.getLocations());
        flyway.setDataSource(dataSource);
        logger.info("****************开始执行数据库脚本更新****************");
        flyway.migrate();
        logger.info("****************数据库脚本升级成功****************");
    }
}
